package xin.pms.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xin.common.utils.AssertUtils;
import xin.lms.annotation.Event;
import xin.pms.ui.UiUser;
import xin.core.jwt.JwtTokenService;
import xin.common.http.response.ListResponse;
import xin.common.http.response.Response;
import xin.common.utils.StringUtils;
import xin.core.AppConstant;
import xin.core.ErrorConstant;
import xin.core.exception.SysException;
import xin.pms.model.Menu;
import xin.pms.model.Permission;
import xin.pms.model.Role;
import xin.pms.model.User;
import xin.pms.service.MenuService;
import xin.pms.service.UserService;

import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * <pre>
 * User Center Controller
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/7/28 16:47
 */
@RestController
@Api(value = "xin-pms", description = "用户中心", position = 3)
@RequestMapping("ucenter")
@Slf4j
public class UserCenterController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private JwtTokenService jwtTokenUtil;

    @Autowired
    private UserService userService;

    @ApiOperation(value = "获取当前用户菜单信息" , notes = "获取当前用户菜单信息",response = ListResponse.class )
    @GetMapping("menus")
    public ListResponse<Menu> menus(@RequestHeader(AppConstant.JWT_HEADER) final String token){
        ListResponse<Menu> response = new ListResponse<>();
        final String userId = jwtTokenUtil.getUserId(token.substring(7));
        response.setData(menuService.getMenuByUserIdAndMenuParentId(userId,Menu.DEFAULT_ROOT_PARENT_ID));
        return response;
    }

    @ApiOperation(value = "获取当前用户信息" , notes = "获取当前用户信息",response = Response.class )
    @GetMapping("user")
    public Response<UiUser> user(@RequestHeader(AppConstant.JWT_HEADER) final String token){
        Response<UiUser> response = new Response<>();
        final String userId = jwtTokenUtil.getUserId(token.substring(7));
        response.setData(new UiUser(userService.findOne(userId)));
        response.setSuccess(Boolean.TRUE);
        return response;
    }

    @ApiOperation(value = "获取当前用户角色" , notes = "获取当前用户角色",response = ListResponse.class )
    @GetMapping("role")
    public ListResponse<Role> role(@RequestHeader(AppConstant.JWT_HEADER) final String token){
        ListResponse<Role> response = new ListResponse<>();
        final String userId = jwtTokenUtil.getUserId(token.substring(7));
        User curr = userService.findOne(userId);
        response.setData(AssertUtils.isNotNull(curr.getAllRoles())
                ?curr.getAllRoles().stream().collect(Collectors.toList())
                :new ArrayList<>());
        response.setSuccess(Boolean.TRUE);
        return response;
    }

    @ApiOperation(value = "获取当前用户权限信息" , notes = "获取当前用户权限信息",response = ListResponse.class )
    @GetMapping("auth/permissions")
    public ListResponse<Permission> permissions(@RequestHeader(AppConstant.JWT_HEADER) final String token){
        ListResponse<Permission> response = new ListResponse<>();
        final String userId = jwtTokenUtil.getUserId(token.substring(7));
        User curr = userService.findOne(userId);
        response.setData(AssertUtils.isNotNull(curr.getAllPermissions())
                ?curr.getAllPermissions().stream().collect(Collectors.toList())
                :new ArrayList<>());
        response.setSuccess(Boolean.TRUE);
        return response;
    }

    @Event(name = "修改密码",desc = "修改密码")
    @ApiOperation(value = "修改密码" , notes = "修改密码",response = Response.class )
    @PostMapping("auth/reset/passwd")
    public Response<String> changePasswd(@ApiParam(name="passwd",value = "新密码")
                                             @RequestParam(name = "passwd")String passwd,
                                             @RequestHeader(AppConstant.JWT_HEADER) final String token){
        Response<String> response = new Response<>();
        response.setSuccess(Boolean.TRUE);
        if(StringUtils.isBlank(passwd)) {
            throw new SysException(ErrorConstant.ERROR_PARAM_0001,"新密码空" );
        }
        final String userId = jwtTokenUtil.getUserId(token.substring(7));
        userService.resetPasswd(userId,passwd);
        return response;
    }


    @Event(name = "重置手机号",desc = "重置手机号")
    @ApiOperation(value = "修改手机号" , notes = "手机号",response = Response.class )
    @PostMapping("auth/reset/mobile")
    public  Response<String> changeMobile( @ApiParam(name="mobile",value = "手机号")
                                               @RequestParam(name = "mobile")String mobile,
                                               @RequestHeader(AppConstant.JWT_HEADER) final String token){
        Response<String> response = new Response<>();
        response.setSuccess(Boolean.TRUE);
        if(StringUtils.isBlank(mobile)) {
            throw new SysException(ErrorConstant.ERROR_PARAM_0001,"变更手机号为空" );
        }
        final String userId = jwtTokenUtil.getUserId(token.substring(7));
        userService.resetMobile(userId,mobile);
        return response;
    }
}